package com.example.learnredis.controller.zset;


import com.example.learnredis.config.RedisConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author Administrator
 */
@RestController
@Slf4j
@RequestMapping(value = "/zset")
public class ZsetController {

    @Resource
    ThreadPoolTaskExecutor executorService;
    //Redis客户端
    private Jedis jedis = new Jedis("127.0.0.1", 6379);
    //店铺销售排行榜
    private static final String amountBang = "tmall:amountBang";
    //店铺订单排行榜
    private static final String orderBang = "tmall:orderBang";

   // @Cacheable(value = RedisConfig.REDIS_KEY_DATABASE, key = "'pms:brand:'+#id")
    @GetMapping(value = "sortBang")
    public LinkedHashMap<String,Object> getNum(Long id){
        log.info("getNum......"+id);
       //创建销售线程-销售商品
        executorService.submit(new Sale());
        executorService.submit(new Sale());
        //创建报表线程-周期型计算排行榜
        executorService.submit(new BangView());
        Set<String> names =jedis.zrevrange(amountBang, 0, 10);
        LinkedHashMap<String,Object>map =  new LinkedHashMap<>();
        for (String name: names) {
            map.put(name,jedis.zscore(amountBang, name));
        }
        return map;
    }
    static class Sale implements Runnable {
        //店铺销售排行榜
        private static final String amountBang = "tmall:amountBang";
        //店铺订单排行榜
        private static final String orderBang = "tmall:orderBang";

        //店铺销售排行榜
        private static final String amountBangTemp = "tmall:amountBangTemp";
        //店铺订单排行榜
        private static final String orderBangTemp = "tmall:orderBangTemp";
        //店铺名称
        private static final String[] shops = new String[]{"小米", "华为", "魅族", "苹果", "联想", "奇酷", "中兴", "一加", "oppo"};
        //随机获取店铺
        private Random random = new Random();
        //随机计算价格
        private Random priceRandom = new Random();
        //Redis客户端
        private Jedis jedis = new Jedis("127.0.0.1", 6379);
        @Autowired
        private RedisTemplate<String, Object> redisTemplate;
        @Override
        public void run() {
                try {
                    int shopIndex = random.nextInt(shops.length);
                    jedis.zincrby(amountBang, priceRandom.nextInt(2500), shops[shopIndex]);
                    jedis.zincrby(orderBang, 1, shops[shopIndex]);
                    redisTemplate.opsForZSet().add(amountBangTemp, shops[shopIndex] ,priceRandom.nextInt(2500));
                    redisTemplate.opsForZSet().add(orderBangTemp, shops[shopIndex],1 );
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println(e);
                }
        }

    }
    class BangView implements Runnable {
        //店铺销售排行榜
        private static final String amountBang = "tmall:amountBang";
        //店铺订单排行榜
        private static final String orderBang = "tmall:orderBang";
        //Redis客户端
        private Jedis jedis = new Jedis("127.0.0.1", 6379);
        @Override
        public void run() {
                try {
                    Thread.sleep(1000);
                    System.out.println("==============店铺销售额排行==============================");
                    Set<String> names = jedis.zrevrange(amountBang, 0, -1);
                    for (String name : names) {
                        System.out.println(name + "         "
                                + jedis.zrevrank(amountBang, name) + "            "
                                + jedis.zscore(amountBang, name));
                    }
                    System.out.println("==============店铺订单量排行==============================");
                    names = jedis.zrevrange(orderBang, 0, 10);
                    for (String name : names) {
                        System.out.println(name + "         "
                                + jedis.zrevrank(orderBang, name) + "            "
                                + jedis.zscore(orderBang, name));
                    }

                    System.out.println();
                    System.out.println();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }

    }
}
